﻿Public Class frmPhanQuyen
    Private Sub frmPhanQuyen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            NhomSuDung_Load()
            'ChucNang_Load()
            CreateTempPermission()
            CreateGrid()
        Catch ex As Exception

        End Try
    End Sub
#Region " Load Data"
    Private Sub CreateGrid()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dtPhanQuyen As New DataTable
            Dim query As String = "select * from phanquyen as P left outer join chucnang as C On P.idchucnang=C.IdChucNang Where GroupId = " & cbxNhom.SelectedValue & " Order By ThuTu ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtPhanQuyen)
            dbConn.Close()

            'bind data
            Grid1.Rows.Clear()
            Grid1.ColumnsCount = 10
            'tạo header 
            Grid1.FixedRows = 1
            Grid1.Rows.Insert(0)
            Grid1(0, 0) = New SourceGrid.Cells.ColumnHeader("STT")
            Grid1(0, 0).Column.Width = 35

            Grid1(0, 1) = New SourceGrid.Cells.ColumnHeader("IdChucNang")
            Grid1(0, 1).Column.Width = 0

            Grid1(0, 2) = New SourceGrid.Cells.ColumnHeader("GroupId")
            Grid1(0, 2).Column.Width = 0

            Grid1(0, 3) = New SourceGrid.Cells.ColumnHeader("Chức năng")
            Grid1(0, 3).Column.Width = 325

            Grid1(0, 4) = New SourceGrid.Cells.ColumnHeader("Xem")
            Grid1(0, 4).Column.Width = 100

            Grid1(0, 5) = New SourceGrid.Cells.ColumnHeader("Thêm mới")
            Grid1(0, 5).Column.Width = 100

            Grid1(0, 6) = New SourceGrid.Cells.ColumnHeader("Chỉnh sửa")
            Grid1(0, 6).Column.Width = 100

            Grid1(0, 7) = New SourceGrid.Cells.ColumnHeader("Xóa")
            Grid1(0, 7).Column.Width = 100

            Grid1(0, 8) = New SourceGrid.Cells.ColumnHeader("Duyệt")
            Grid1(0, 8).Column.Width = 100

            Grid1(0, 9) = New SourceGrid.Cells.ColumnHeader("In")
            Grid1(0, 9).Column.Width = 100
            'style header
            Dim rowCOLSPAN As New SourceGrid.Cells.Views.Cell
            rowCOLSPAN.BackColor = Color.FromArgb(192, 255, 192)
            'rowHeThong
            Dim rowHeThong() As DataRow
            rowHeThong = dtPhanQuyen.Select("levelmenu='HeThong'")
            'header
            Grid1.Rows.Insert(1)
            Grid1(1, 0) = New SourceGrid.Cells.Cell("Nhóm chức năng : Hệ thống")
            Grid1(1, 0).ColumnSpan = 10
            Grid1(1, 0).View = rowCOLSPAN
            Dim k_rowInserted As Integer = 2
            'sort
            For i As Integer = 0 To rowHeThong.Length - 1
                Grid1.Rows.Insert(i + 2)
                k_rowInserted = k_rowInserted + 1
                'STT
                Grid1(i + 2, 0) = New SourceGrid.Cells.Cell((i + 1).ToString)
                Grid1(i + 2, 0).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
                'IdChucNang
                Grid1(i + 2, 1) = New SourceGrid.Cells.Cell(rowHeThong(i)("IDChucNang"))
                'GroupId
                Grid1(i + 2, 2) = New SourceGrid.Cells.Cell(rowHeThong(i)("GroupId"))

                'ChucNang
                Grid1(i + 2, 3) = New SourceGrid.Cells.Cell(rowHeThong(i)("TenChucNang"))
                Grid1(i + 2, 3).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft
                'Xem
                Grid1(i + 2, 4) = New SourceGrid.Cells.CheckBox("", IIf(rowHeThong(i)("Xem") = "1", True, False))
                'ThemMoi
                Grid1(i + 2, 5) = New SourceGrid.Cells.CheckBox("", IIf(rowHeThong(i)("ThemMoi") = "1", True, False))
                'ChinhSua
                Grid1(i + 2, 6) = New SourceGrid.Cells.CheckBox("", IIf(rowHeThong(i)("ChinhSua") = "1", True, False))
                'Xoa
                Grid1(i + 2, 7) = New SourceGrid.Cells.CheckBox("", IIf(rowHeThong(i)("Xoa") = "1", True, False))
                'Duyet
                Grid1(i + 2, 8) = New SourceGrid.Cells.CheckBox("", IIf(rowHeThong(i)("Duyet") = "1", True, False))
                'InData
                Grid1(i + 2, 9) = New SourceGrid.Cells.CheckBox("", IIf(rowHeThong(i)("InData") = "1", True, False))
            Next
            'header
            'Quản lý bán hàng
            Grid1.Rows.Insert(k_rowInserted)
            Grid1(k_rowInserted, 0) = New SourceGrid.Cells.Cell("Nhóm chức năng : Quản lý bán hàng")
            Grid1(k_rowInserted, 0).ColumnSpan = 10
            Grid1(k_rowInserted, 0).View = rowCOLSPAN
            Dim rowBanHang() As DataRow
            rowBanHang = dtPhanQuyen.Select("levelmenu='BanHang'")
            k_rowInserted = k_rowInserted + 1
            For i As Integer = 0 To rowBanHang.Length - 1
                Grid1.Rows.Insert(k_rowInserted)
                'STT
                Grid1(k_rowInserted, 0) = New SourceGrid.Cells.Cell((i + 1).ToString)
                'IdChucNang
                Grid1(k_rowInserted, 1) = New SourceGrid.Cells.Cell(rowBanHang(i)("IDChucNang"))
                'GroupId
                Grid1(k_rowInserted, 2) = New SourceGrid.Cells.Cell(rowBanHang(i)("GroupId"))

                'ChucNang
                Grid1(k_rowInserted, 3) = New SourceGrid.Cells.Cell(rowBanHang(i)("TenChucNang"))
                'Xem
                Grid1(k_rowInserted, 4) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("Xem") = "1", True, False))
                'ThemMoi
                Grid1(k_rowInserted, 5) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("ThemMoi") = "1", True, False))
                'ChinhSua
                Grid1(k_rowInserted, 6) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("ChinhSua") = "1", True, False))
                'Xoa
                Grid1(k_rowInserted, 7) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("Xoa") = "1", True, False))
                'Duyet
                Grid1(k_rowInserted, 8) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("Duyet") = "1", True, False))
                'InData
                Grid1(k_rowInserted, 9) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("InData") = "1", True, False))
                k_rowInserted = k_rowInserted + 1
            Next
            'Quản lý kho
            Grid1.Rows.Insert(k_rowInserted)
            Grid1(k_rowInserted, 0) = New SourceGrid.Cells.Cell("Nhóm chức năng : Quản lý kho")
            Grid1(k_rowInserted, 0).ColumnSpan = 10
            Grid1(k_rowInserted, 0).View = rowCOLSPAN
            rowBanHang = dtPhanQuyen.Select("levelmenu='Kho'")
            k_rowInserted = k_rowInserted + 1
            For i As Integer = 0 To rowBanHang.Length - 1
                Grid1.Rows.Insert(k_rowInserted)
                'STT
                Grid1(k_rowInserted, 0) = New SourceGrid.Cells.Cell((i + 1).ToString)
                'IdChucNang
                Grid1(k_rowInserted, 1) = New SourceGrid.Cells.Cell(rowBanHang(i)("IDChucNang"))
                'GroupId
                Grid1(k_rowInserted, 2) = New SourceGrid.Cells.Cell(rowBanHang(i)("GroupId"))

                'ChucNang
                Grid1(k_rowInserted, 3) = New SourceGrid.Cells.Cell(rowBanHang(i)("TenChucNang"))
                'Xem
                Grid1(k_rowInserted, 4) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("Xem") = "1", True, False))
                'ThemMoi
                Grid1(k_rowInserted, 5) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("ThemMoi") = "1", True, False))
                'ChinhSua
                Grid1(k_rowInserted, 6) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("ChinhSua") = "1", True, False))
                'Xoa
                Grid1(k_rowInserted, 7) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("Xoa") = "1", True, False))
                'Duyet
                Grid1(k_rowInserted, 8) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("Duyet") = "1", True, False))
                'InData
                Grid1(k_rowInserted, 9) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("InData") = "1", True, False))
                k_rowInserted = k_rowInserted + 1
            Next
            'Kế toán
            Grid1.Rows.Insert(k_rowInserted)
            Grid1(k_rowInserted, 0) = New SourceGrid.Cells.Cell("Nhóm chức năng : Kế toán")
            Grid1(k_rowInserted, 0).ColumnSpan = 10
            Grid1(k_rowInserted, 0).View = rowCOLSPAN
            rowBanHang = dtPhanQuyen.Select("levelmenu='KeToan'")
            k_rowInserted = k_rowInserted + 1
            For i As Integer = 0 To rowBanHang.Length - 1
                Grid1.Rows.Insert(k_rowInserted)
                'STT
                Grid1(k_rowInserted, 0) = New SourceGrid.Cells.Cell((i + 1).ToString)
                'IdChucNang
                Grid1(k_rowInserted, 1) = New SourceGrid.Cells.Cell(rowBanHang(i)("IDChucNang"))
                'GroupId
                Grid1(k_rowInserted, 2) = New SourceGrid.Cells.Cell(rowBanHang(i)("GroupId"))

                'ChucNang
                Grid1(k_rowInserted, 3) = New SourceGrid.Cells.Cell(rowBanHang(i)("TenChucNang"))
                'Xem
                Grid1(k_rowInserted, 4) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("Xem") = "1", True, False))
                'ThemMoi
                Grid1(k_rowInserted, 5) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("ThemMoi") = "1", True, False))
                'ChinhSua
                Grid1(k_rowInserted, 6) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("ChinhSua") = "1", True, False))
                'Xoa
                Grid1(k_rowInserted, 7) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("Xoa") = "1", True, False))
                'Duyet
                Grid1(k_rowInserted, 8) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("Duyet") = "1", True, False))
                'InData
                Grid1(k_rowInserted, 9) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("InData") = "1", True, False))
                k_rowInserted = k_rowInserted + 1
            Next
            'Quản lý
            Grid1.Rows.Insert(k_rowInserted)
            Grid1(k_rowInserted, 0) = New SourceGrid.Cells.Cell("Nhóm chức năng : Thống kê-Báo cáo")
            Grid1(k_rowInserted, 0).ColumnSpan = 10
            Grid1(k_rowInserted, 0).View = rowCOLSPAN
            rowBanHang = dtPhanQuyen.Select("levelmenu='TruongPhong'")
            k_rowInserted = k_rowInserted + 1
            For i As Integer = 0 To rowBanHang.Length - 1
                Grid1.Rows.Insert(k_rowInserted)
                'STT
                Grid1(k_rowInserted, 0) = New SourceGrid.Cells.Cell((i + 1).ToString)
                'IdChucNang
                Grid1(k_rowInserted, 1) = New SourceGrid.Cells.Cell(rowBanHang(i)("IDChucNang"))
                'GroupId
                Grid1(k_rowInserted, 2) = New SourceGrid.Cells.Cell(rowBanHang(i)("GroupId"))

                'ChucNang
                Grid1(k_rowInserted, 3) = New SourceGrid.Cells.Cell(rowBanHang(i)("TenChucNang"))
                'Xem
                Grid1(k_rowInserted, 4) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("Xem") = "1", True, False))
                'ThemMoi
                Grid1(k_rowInserted, 5) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("ThemMoi") = "1", True, False))
                'ChinhSua
                Grid1(k_rowInserted, 6) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("ChinhSua") = "1", True, False))
                'Xoa
                Grid1(k_rowInserted, 7) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("Xoa") = "1", True, False))
                'Duyet
                Grid1(k_rowInserted, 8) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("Duyet") = "1", True, False))
                'InData
                Grid1(k_rowInserted, 9) = New SourceGrid.Cells.CheckBox("", IIf(rowBanHang(i)("InData") = "1", True, False))
                k_rowInserted = k_rowInserted + 1
            Next
        Catch ex As Exception

        End Try
    End Sub
    Public Sub NhomSuDung_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsNhanVienTiepThi As New DataSet
            Dim dtNhanVienTiepThi As New DataTable
            Dim query As String = "Select * From [Group]"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsNhanVienTiepThi)
            dbConn.Close()
            dtNhanVienTiepThi = dsNhanVienTiepThi.Tables(0)
            dtNhanVienTiepThi.Columns.Add("STT")
            cbxNhom.DataSource = dtNhanVienTiepThi
            cbxNhom.DisplayMember = "GroupName"
            cbxNhom.ValueMember = "GroupId"
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Public Sub ChucNang_Load()
        'Try
        '    Dim dbConn As New OleDb.OleDbConnection
        '    Dim dataAccess As New DataAccess
        '    Dim dsNhanVienTiepThi As New DataSet
        '    Dim dtNhanVienTiepThi As New DataTable
        '    Dim query As String = "select * from phanquyen as P left outer join chucnang as C On P.idchucnang=C.IdChucNang Where GroupId = " & cbxNhom.SelectedValue
        '    Dim adapter As OleDb.OleDbDataAdapter
        '    dataAccess.ConnectDatabase_DBAccess(dbConn)
        '    adapter = New OleDb.OleDbDataAdapter(query, dbConn)
        '    adapter.Fill(dsNhanVienTiepThi)
        '    dbConn.Close()
        '    dtNhanVienTiepThi = dsNhanVienTiepThi.Tables(0)
        '    dtNhanVienTiepThi.Columns.Add("STT")
        '    For i As Integer = 0 To dtNhanVienTiepThi.Rows.Count - 1
        '        dtNhanVienTiepThi.Rows(i)("STT") = i + 1
        '    Next
        '    dgvPhanQuyen.AutoGenerateColumns = False
        '    dgvPhanQuyen.DataSource = dtNhanVienTiepThi
        '    'set focus to first row
        '    If dgvPhanQuyen.Rows.Count - 1 > 0 Then
        '        dgvPhanQuyen.Rows(0).Selected = True
        '    End If
        'Catch ex As Exception
        '    ShowError()
        'End Try
    End Sub
#End Region
    Private Sub CreateTempPermission()
        Try
            'Lất cả các nhóm quyền mà chưa được phân quyền
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess          
            Dim dtChuaPhanQuyen As New DataTable
            Dim query As String = "select groupid from [group] where groupid not in (select distinct groupid from phanquyen)"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtChuaPhanQuyen)
            'lấy danh mục nhóm chức năng
            Dim dtChucNang As New DataTable
            query = "select idchucnang from chucnang"
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtChucNang)
            Dim groupid As Integer
            Dim dbCommand As OleDb.OleDbCommand
            For i As Integer = 0 To dtChuaPhanQuyen.Rows.Count - 1
                'phan quyen cho tung group
                groupid = dtChuaPhanQuyen.Rows(i)("groupid")
                For j As Integer = 0 To dtChucNang.Rows.Count - 1
                    query = String.Format("Insert Into PhanQuyen(idchucnang,groupid,xem,themmoi,chinhsua,xoa,duyet,indata) values(N'{0}',{1},{2},{3},{4},{5},{6},{7})", dtChucNang.Rows(j)("idchucnang"), groupid, 0, 0, 0, 0, 0, 0)
                    dbCommand = New OleDb.OleDbCommand(query, dbConn)
                    dbCommand.ExecuteNonQuery()
                Next
            Next
            'Lấy các chức năng chưa dc phân quyền
            dtChucNang = New DataTable
            query = "select idchucnang from [chucnang] where idchucnang not in (select distinct idchucnang from phanquyen)"
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtChucNang)
            dtChuaPhanQuyen = New DataTable
            query = "select groupid from [group]"
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtChuaPhanQuyen)
            'phân quyền chức năng đó
            For i As Integer = 0 To dtChuaPhanQuyen.Rows.Count - 1
                'phan quyen cho tung group
                groupid = dtChuaPhanQuyen.Rows(i)("groupid")
                For j As Integer = 0 To dtChucNang.Rows.Count - 1
                    query = String.Format("Insert Into PhanQuyen(idchucnang,groupid,xem,themmoi,chinhsua,xoa,duyet,indata) values(N'{0}',{1},{2},{3},{4},{5},{6},{7})", dtChucNang.Rows(j)("idchucnang"), groupid, 0, 0, 0, 0, 0, 0)
                    dbCommand = New OleDb.OleDbCommand(query, dbConn)
                    dbCommand.ExecuteNonQuery()
                Next
            Next

            query = "select distinct groupid from phanquyen where groupid not in (select groupid from [group])"
            Dim dtNhomKhongSuDung As New DataTable
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtNhomKhongSuDung)
            For k As Integer = 0 To dtNhomKhongSuDung.Rows.Count - 1
                query = "Delete PhanQuyen Where GroupId=" & dtNhomKhongSuDung.Rows(k)("groupid")
                dbCommand = New OleDb.OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
            Next
            dbConn.Close()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim query As String = ""
        Try
            Dim IdChucnang As String = ""
            Dim GroupId As Integer = 0
            Dim Xem As Integer = 0
            Dim ThemMoi As Integer = 0
            Dim ChinhSua As Integer = 0
            Dim Xoa As Integer = 0
            Dim Duyet As Integer = 0
            Dim InData As Integer = 0
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            For i As Integer = 2 To Grid1.RowsCount - 1
                If Grid1(i, 0).ColumnSpan = 10 Then
                    Continue For
                Else
                    Dim k As Integer = i
                    IdChucnang = Grid1(i, 1).Value
                    GroupId = cbxNhom.SelectedValue
                    If Grid1(i, 4).Value = True Then
                        Xem = 1
                    Else
                        Xem = 0
                    End If
                    If Grid1(i, 5).Value = True Then
                        ThemMoi = 1
                    Else
                        ThemMoi = 0
                    End If
                    If Grid1(i, 6).Value = True Then
                        ChinhSua = 1
                    Else
                        ChinhSua = 0
                    End If
                    If Grid1(i, 7).Value = True Then
                        Xoa = 1
                    Else
                        Xoa = 0
                    End If
                    If Grid1(i, 8).Value = True Then
                        Duyet = 1
                    Else
                        Duyet = 0
                    End If
                    If Grid1(i, 9).Value = True Then
                        InData = 1
                    Else
                        InData = 0
                    End If
                    query = "Update PhanQuyen Set Xem=" & Xem & ",ThemMoi=" & ThemMoi & ",ChinhSua=" & ChinhSua & ",Xoa=" & Xoa & ",Duyet=" & Duyet & ",InData=" & InData & " Where GroupId=" & GroupId & " And IdChucNang=N'" & IdChucnang & "'"
                    dbCommand = New OleDb.OleDbCommand(query, dbConn)
                    dbCommand.ExecuteNonQuery()
                End If
            Next
            dbConn.Close()
            ChucNang_Load()
            'Load Lại Phân Quyền
            PhanQuyenMenu(frmMain.UserId)
            MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK)
        Catch ex As Exception
            MessageBox.Show(ex.InnerException.ToString() & " " & query)
        End Try
    End Sub

    Private Sub cbxNhom_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxNhom.SelectionChangeCommitted
        Try
            CreateGrid()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Try
            Me.Close()
        Catch ex As Exception

        End Try
    End Sub
End Class